package com.ruoyi.service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.entity.BizBusinessPktToCloud;
import com.ruoyi.mapper.BizBusinessPktToCloudMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.bean.BizBusinessPktToCloudFrom;
import com.sxftech.resp.ResponseData;

import java.util.List;

/**
 * <p>
 * 本地端和云端业务关系表 服务实现类
 * </p>
 *
 * @author liuxing
 * @since 2020-10-20
 */
@Service
@DataSource(value = DataSourceType.SLAVE)  //切换数据源
public class BizBusinessPktToCloudService extends ServiceImpl<BizBusinessPktToCloudMapper, BizBusinessPktToCloud> {

	public ResponseData create(BizBusinessPktToCloudFrom bizBusinessPktToCloud) {
		BizBusinessPktToCloud entity = transformationFrom(bizBusinessPktToCloud);
		this.save(entity);
		return ResponseData.success("新增成功");
	}
	
	public ResponseData modify(BizBusinessPktToCloudFrom bizBusinessPktToCloud) {
		if(findById(bizBusinessPktToCloud.getId())== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		BizBusinessPktToCloud entity = transformationFrom(bizBusinessPktToCloud);
		entity.setId(bizBusinessPktToCloud.getId());
		this.updateById(entity);
		return ResponseData.success("修改成功");
	}

	public BizBusinessPktToCloud transformationFrom(BizBusinessPktToCloudFrom bizBusinessPktToCloud) {
		BizBusinessPktToCloud entity = new BizBusinessPktToCloud();
		entity.setBizId(bizBusinessPktToCloud.getBizId());
		entity.setCarBusinessId(bizBusinessPktToCloud.getCarBusinessId());
		entity.setSpaceBusinessId(bizBusinessPktToCloud.getSpaceBusinessId());
		entity.setType(bizBusinessPktToCloud.getType());
		entity.setPlCode(bizBusinessPktToCloud.getPlCode());
		entity.setPlateNumber(bizBusinessPktToCloud.getPlateNumber());
		entity.setCreator(bizBusinessPktToCloud.getCreator());
		entity.setCreateTime(bizBusinessPktToCloud.getCreateTime());
		entity.setModifier(bizBusinessPktToCloud.getModifier());
		entity.setModifyTime(bizBusinessPktToCloud.getModifyTime());
		return entity;
	}

	public BizBusinessPktToCloudFrom transformationBean(BizBusinessPktToCloud bizBusinessPktToCloud) {
		BizBusinessPktToCloudFrom from = new BizBusinessPktToCloudFrom();
		from.setBizId(bizBusinessPktToCloud.getBizId());
		from.setCarBusinessId(bizBusinessPktToCloud.getCarBusinessId());
		from.setSpaceBusinessId(bizBusinessPktToCloud.getSpaceBusinessId());
		from.setType(bizBusinessPktToCloud.getType());
		from.setPlCode(bizBusinessPktToCloud.getPlCode());
		from.setPlateNumber(bizBusinessPktToCloud.getPlateNumber());
		from.setCreator(bizBusinessPktToCloud.getCreator());
		from.setCreateTime(bizBusinessPktToCloud.getCreateTime());
		from.setModifier(bizBusinessPktToCloud.getModifier());
		from.setModifyTime(bizBusinessPktToCloud.getModifyTime());

		return from;
	}

	
	public BizBusinessPktToCloudFrom findById(String id) {
		BizBusinessPktToCloud entity = this.getOne(new QueryWrapper<BizBusinessPktToCloud>().lambda()
				.eq(BizBusinessPktToCloud::getId, id)
				);
		if(entity == null) return null;
		return transformationBean(entity);
	}
	
	public ResponseData remove(String id) {
		if(findById(id)== null) {
			return ResponseData.fail("没有找到对应记录");
		}
		BizBusinessPktToCloud bizBusinessPktToCloud = new BizBusinessPktToCloud();
		bizBusinessPktToCloud.setId(id);
		this.updateById(bizBusinessPktToCloud);
		return ResponseData.success("删除成功");
	}
	
	public IPage pageList(Integer page, Integer size, BizBusinessPktToCloudFrom ibean) {
		return this.page(new Page(page, size), new QueryWrapper<BizBusinessPktToCloud>().lambda()
				.eq(ibean.getBizId() != null, BizBusinessPktToCloud::getBizId, ibean.getBizId())
				.eq(ibean.getCarBusinessId() != null, BizBusinessPktToCloud::getCarBusinessId, ibean.getCarBusinessId())
				.eq(ibean.getSpaceBusinessId() != null, BizBusinessPktToCloud::getSpaceBusinessId, ibean.getSpaceBusinessId())
				.eq(StringUtils.isNotEmpty(ibean.getType()), BizBusinessPktToCloud::getType, ibean.getType())
				.eq(StringUtils.isNotEmpty(ibean.getPlCode()), BizBusinessPktToCloud::getPlCode, ibean.getPlCode())
				.eq(StringUtils.isNotEmpty(ibean.getPlateNumber()), BizBusinessPktToCloud::getPlateNumber, ibean.getPlateNumber())
				.eq(ibean.getCreator() != null, BizBusinessPktToCloud::getCreator, ibean.getCreator())
				.eq(ibean.getCreateTime() != null, BizBusinessPktToCloud::getCreateTime, ibean.getCreateTime())
				.eq(ibean.getModifier() != null, BizBusinessPktToCloud::getModifier, ibean.getModifier())
				.eq(ibean.getModifyTime() != null, BizBusinessPktToCloud::getModifyTime, ibean.getModifyTime())
				);
	}

	public List<BizBusinessPktToCloud> oneList(Integer bizId,String plCode,String plateNumber){
		QueryWrapper<BizBusinessPktToCloud> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("biz_id",bizId);
		queryWrapper.eq("pl_code",plCode);
		queryWrapper.eq("plate_number",plateNumber);
		return list(queryWrapper);
	}
}
